package com.zhoug.common3.clip;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * @Author: zhoug
 * @Date: 2022/9/6
 * @Description:
 */
public class ClipImageHelper {
    /**
     * 选择框是否可以拖动
     */
    private boolean frameTransEnable = true;
    /**
     * 选择框是否可以缩放
     */
    private boolean frameScaleEnable = true;
    /**
     * 选择框默认宽高比
     */
    private float frameRatio = 0.75f;
    /**
     * 需要剪切的图片路径
     */
    private String inputData;
    /**
     * 剪切后的图片输出路径
     */
    private String outputData;
    /**
     * 裁剪后输出图片存储格式,未设置outputData时自动生成路径时使用
     * JPEG,PNG,WEBP
     * {@link Bitmap.CompressFormat#JPEG#toString()}
     * {@link Bitmap.CompressFormat#PNG#toString()}
     * {@link Bitmap.CompressFormat#WEBP#toString()}
     */
    private String outputFormat;

    private Object target;

    public static ClipImageHelper with(@NonNull Activity activity) {
        return new ClipImageHelper(activity);
    }

    public static ClipImageHelper with(@NonNull Fragment fragment) {
        return new ClipImageHelper(fragment);
    }


    private ClipImageHelper(Object target) {
        this.target = target;
    }



    public ClipImageHelper setInputData(String inputData) {
        this.inputData = inputData;
        return this;
    }

    public ClipImageHelper setOutputData(String outputData) {
        this.outputData = outputData;
        return this;
    }

    public ClipImageHelper setOutputFormat(String outputFormat) {
        this.outputFormat = outputFormat;
        return this;
    }

    public ClipImageHelper setFrameTransEnable(boolean frameTransEnable) {
        this.frameTransEnable = frameTransEnable;
        return this;
    }

    public ClipImageHelper setFrameScaleEnable(boolean frameScaleEnable) {
        this.frameScaleEnable = frameScaleEnable;
        return this;
    }

    public ClipImageHelper setFrameRatio(float frameRatio) {
        this.frameRatio = frameRatio;
        return this;
    }

    public void start(int requestCode) {
        Context context = null;
        if (target instanceof Activity) {
            context = (Context) target;
        } else if (target instanceof Fragment) {
            context = ((Fragment) target).getContext();
        }
        Intent intent = new Intent(context, ClipImageActivity.class);
        intent.putExtra(ClipImageActivity.EXTRA_INPUT_DATA, inputData);
        intent.putExtra(ClipImageActivity.EXTRA_OUTPUT_DATA, outputData);
        intent.putExtra(ClipImageActivity.EXTRA_OUTPUT_FORMAT, outputFormat);
        intent.putExtra(ClipImageActivity.EXTRA_FRAME_TRANS_ENABLE, frameTransEnable);
        intent.putExtra(ClipImageActivity.EXTRA_FRAME_SCALE_ENABLE, frameScaleEnable);
        intent.putExtra(ClipImageActivity.EXTRA_FRAME_RATIO, frameRatio);
        if (target instanceof Activity) {
            ((Activity) target).startActivityForResult(intent, requestCode);
        } else if (target instanceof Fragment) {
            ((Fragment) target).startActivityForResult(intent, requestCode);
        }
    }


    /**
     * 在Activity.onActivityResult(int, int, Intent)}中调用
     *
     * @return
     */
    public static String parseResult(@Nullable Intent data) {
        if (data != null) {
            return data.getStringExtra(ClipImageActivity.EXTRA_OUTPUT_DATA);
        }
        return null;
    }


}
